package com.harmony.pilot.analysis.controller;

import com.harmony.pilot.common.dto.AnalysisRequest;
import com.harmony.pilot.common.dto.AnalysisResult;
import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.analysis.service.AnalysisService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 代码分析控制器
 */
@Slf4j
@RestController
@RequestMapping("/analysis")
@RequiredArgsConstructor
public class AnalysisController {
    
    private final AnalysisService analysisService;
    
    /**
     * 分析代码
     */
    @PostMapping("/analyze")
    public RestResult<AnalysisResult> analyzeCode(@Valid @RequestBody AnalysisRequest request) {
        log.info("Received code analysis request for file: {}", 
                request.getContext().getFilePath());
        
        try {
            AnalysisResult result = analysisService.analyzeCode(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to analyze code", e);
            return RestResult.error("Failed to analyze code: " + e.getMessage());
        }
    }
    
    /**
     * 语法检查
     */
    @PostMapping("/syntax")
    public RestResult<AnalysisResult> checkSyntax(@Valid @RequestBody AnalysisRequest request) {
        log.info("Received syntax check request");
        
        try {
            AnalysisResult result = analysisService.checkSyntax(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to check syntax", e);
            return RestResult.error("Failed to check syntax: " + e.getMessage());
        }
    }
    
    /**
     * 代码风格检查
     */
    @PostMapping("/style")
    public RestResult<AnalysisResult> checkStyle(@Valid @RequestBody AnalysisRequest request) {
        log.info("Received style check request");
        
        try {
            AnalysisResult result = analysisService.checkStyle(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to check style", e);
            return RestResult.error("Failed to check style: " + e.getMessage());
        }
    }
    
    /**
     * 安全检查
     */
    @PostMapping("/security")
    public RestResult<AnalysisResult> checkSecurity(@Valid @RequestBody AnalysisRequest request) {
        log.info("Received security check request");
        
        try {
            AnalysisResult result = analysisService.checkSecurity(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to check security", e);
            return RestResult.error("Failed to check security: " + e.getMessage());
        }
    }
    
    /**
     * 性能检查
     */
    @PostMapping("/performance")
    public RestResult<AnalysisResult> checkPerformance(@Valid @RequestBody AnalysisRequest request) {
        log.info("Received performance check request");
        
        try {
            AnalysisResult result = analysisService.checkPerformance(request);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to check performance", e);
            return RestResult.error("Failed to check performance: " + e.getMessage());
        }
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public RestResult<String> health() {
        return RestResult.success("Code Analysis Service is running");
    }
}

